
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyJoinColumn;
import javax.persistence.OneToMany;

/**
 *
 * @author Michal Vachler
 */
@Entity
public class Recept implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(nullable = false)
    private String nazev;
    @Column(nullable = false)
    private List<Krok> postup;
    @ManyToOne
    @Column(nullable = false)
    private Uzivatel uzivatel;
    private Krok krok;

    @ManyToMany
    @MapKeyJoinColumn(name="INGREDIENCE")
    private Map <Ingredience, Mnozstvi> seznamIngredienci;

    public Recept(String nazev, List<Krok> postup, Uzivatel uzivatel, Map<Ingredience, Mnozstvi> seznamIngredienci) {
        this.nazev = nazev;
        this.postup = postup;
        this.uzivatel = uzivatel;
        this.seznamIngredienci = seznamIngredienci;
    }

    public Recept() {
    }
 
    public Long getId() {
        return id;
    }
    
    public void posunKrok(int puvodniPoradi, int novePoradi) {
        boolean zmena = false;
        
        if (novePoradi < puvodniPoradi) {
            zmena = true;        
        }
               
        for (Krok k : postup) {
            if (k.getPoradi() == puvodniPoradi) {
                for (Krok j : postup) {
                    if (zmena && j.getPoradi() < puvodniPoradi) {
                        j.setPoradi(j.getPoradi()+1);
                    } else if (!zmena && j.getPoradi() > puvodniPoradi) {
                        j.setPoradi(j.getPoradi()-1);
                    }
                }
                k.setPoradi(novePoradi);
            break;
            }
        }
    }
    
    public void pridatKrok(String instrukce) {
        Krok krok = new Krok(postup.size()+1, instrukce);
        postup.add(krok);
    }
    
    public void odeberKrok(String krok) {
       Krok smazat = null;
       
       for (Krok k : postup) {
           if (k.getKrok().equals(krok)) {
               smazat = k;
           }
           if (smazat != null) {
               k.setPoradi(k.getPoradi()-1);
           }
       }
       
       if (smazat != null) {
           postup.remove(smazat);
       }
   }

    public Map<Ingredience, Mnozstvi> getSeznamIngredienci() {
        return seznamIngredienci;
    }

    public boolean pridatIngredienci(Ingredience ingredience, Mnozstvi mnozstvi){
        try {
            seznamIngredienci.put(ingredience, mnozstvi);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    
    public boolean odebratIngredienci(Ingredience ingredience){
        try {
            seznamIngredienci.remove(ingredience);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNazev() {
        return nazev;
    }

    public void setNazev(String nazev) {
        this.nazev = nazev;
    }

    public List<Krok> getPostup() {
        return postup;
    }

    public void setPostup(List<Krok> postup) {
        this.postup = postup;
    }

   public String vratitKrok(int indexKroku) {
            for (Krok k : postup) {
                if (k.getPoradi() == indexKroku) {
                    krok = k;   
            }  
            }
        return krok.getKrok();
    }
    
    public Uzivatel getUzivatel() {
        return uzivatel;
    }

    public void setUzivatel(Uzivatel uzivatel) {
        this.uzivatel = uzivatel;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Recept other = (Recept) obj;
        if (!Objects.equals(this.id, other.id)) {
            return false;
        }
        if (!Objects.equals(this.nazev, other.nazev)) {
            return false;
        }
        if (!Objects.equals(this.postup, other.postup)) {
            return false;
        }
        if (!Objects.equals(this.seznamIngredienci, other.seznamIngredienci)) {
            return false;
        }
        return true;
    }



    @Override
    public String toString() {
        return "entities.Recept[ id=" + id + " ]";
    }
    
}